package com.topone.controller;


import com.topone.Exception.FanZoneException;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.ResultVO;
import com.topone.dto.BussinessDTO;
import com.topone.service.BussinessService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/bussiness")
@RestController
@Slf4j
@CrossOrigin
public class LoginController {

    @Autowired
    private BussinessService bussinessService;

    @PostMapping("/login")
    public ResultVO<Map<String, String>> bussinessLogin(@RequestParam("bussinessPhone") String bussinessPhone,
                                                        @RequestParam("bussinessPassword") String bussinessPassword,
                                                        Map<String, String> map) {

        if (StringUtils.isNotEmpty(bussinessPhone) && StringUtils.isNotEmpty(bussinessPassword)) {
            try {
                BussinessDTO bussinessDTO = bussinessService.checkBussinessPassword(bussinessPassword, bussinessPhone);
                log.info("{}",bussinessDTO);
                if(bussinessDTO!=null){
                    map.put("bussinessId", bussinessDTO.getBussinessId());
                }else{
                 map.put("error","帐号不存在或密码不正确");
                }
            } catch (FanZoneException e) {
                log.error("【校验商家核销登录】失败", e);
                map.put("error", e.getMessage());
                return ResultVOUntil.error(1, e.getMessage());
            }
        }else{
            map.put("error","参数为空");
            return ResultVOUntil.error(1,"参数为空");
        }
        log.info("【校验商家核销登录】成功，bussinessId={}", map.get("bussinessId"));
        return ResultVOUntil.success(map);
    }

}
